package com.zrrd.yunchmall.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.yunchmall.product.entity.ProductAttribute;
import com.zrrd.yunchmall.product.entity.ProductCategory;
import com.zrrd.yunchmall.product.service.IProductCategoryService;
import com.zrrd.yunchmall.product.service.IProductService;
import com.zrrd.yunchmall.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 产品分类 前端控制器
 * </p>
 *
 * @author YeKong
 * @since 2024-01-15
 */
@RestController
@RequestMapping("/productCategory")
@Api(tags = "商品服务-商品类别模块")
public class ProductCategoryController {
    @Autowired
    private IProductCategoryService categoryService;

    @ApiOperation("查找商品类别及其子类别")
    @GetMapping("/list/withChildren")
    public ResponseResult listWithChildren() {
        List<ProductCategory> categories = categoryService.listWithChildren();
        return new ResponseResult(200,"查询成功",categories);
    }


    @ApiOperation("查找商品类别")
    @GetMapping("/list/{level}")
    public ResponseResult list(@PathVariable("level") int level,
                               int pageNum, int pageSize) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("level", level);
        return new ResponseResult(200, "查询成功过",
                categoryService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    @ApiOperation("是否导航")
    @PostMapping("/update/navStatus")
    public ResponseResult updateNavStatus(String ids, Integer status) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.set("nav_status", status);
        updateWrapper.in("id", ids.split(","));
        categoryService.update(updateWrapper);
        return new ResponseResult(200, "更改成功");
    }

    @ApiOperation("是否显示")
    @PostMapping("/update/showStatus")
    public ResponseResult updateShowStatus(String ids, Integer status) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.set("show_status", status);
        updateWrapper.in("id", ids.split(","));
        categoryService.update(updateWrapper);
        return new ResponseResult(200, "更改成功");
    }

    @ApiOperation("获取信息")
    @GetMapping("/{id}")
    public ResponseResult get(@PathVariable("id") long id) {
        return new ResponseResult(200, "获取成功",
                categoryService.getById(id));
    }

    @ApiOperation("创建")
    @PostMapping("/create")
    public ResponseResult create(@RequestBody ProductCategory productCategory) {
        if (productCategory.getParentId() == 0) {
            productCategory.setLevel(0);
        }else {
            productCategory.setLevel(1);
        }
        categoryService.create(productCategory);
        return new ResponseResult(200, "创建成功");
    }

    @ApiOperation("删除")
    @PostMapping("/delete/{id}")
    public ResponseResult delete(@PathVariable("id") long id) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", id);
        categoryService.remove(updateWrapper);
        return new ResponseResult(200, "删除成功");
    }

    @ApiOperation("更改")
    @PostMapping("/update/{id}")
    public ResponseResult update(@PathVariable("id") long id,
                                 @RequestBody ProductCategory productCategory) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", id);
        categoryService.update(productCategory, updateWrapper);
        return new ResponseResult(200, "更改成功");
    }
}
